<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.paths.core }}css/login.css?v={{ layui.admin.v }}" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>登录</h2>
            <p></p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form" lay-filter="LAY-user-login">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                       for="LAY-user-login-username"></label>
                <input type="text" name="username" id="LAY-user-login-username" lay-verify="required"
                       placeholder="用户名" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                       for="LAY-user-login-password"></label>
                <input type="password" name="password" id="LAY-user-login-password" lay-verify="required"
                       placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                               for="LAY-user-login-smscode"></label>
                        <input type="text" name="code" id="LAY-user-login-smscode" lay-verify="required"
                               placeholder="邮箱验证码" class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid"
                                    id="LAY-user-login-getsmscode">获取验证码
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <!--      <div class="layui-form-item">-->
            <!--        <div class="layui-row">-->
            <!--          <div class="layui-col-xs7">-->
            <!--            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>-->
            <!--            <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required" placeholder="图形验证码" class="layui-input">-->
            <!--          </div>-->
            <!--          <div class="layui-col-xs5">-->
            <!--            <div style="margin-left: 10px;">-->
            <!--              <img src="https://www.oschina.net/action/user/captcha" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">-->
            <!--            </div>-->
            <!--          </div>-->
            <!--        </div>-->
            <!--      </div>-->
            <div class="layui-form-item" style="margin-bottom: 20px;">
                <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
                <a lay-href="/user/forget" class="layadmin-user-jump-change layadmin-link" style="margin-top: 7px;">忘记密码？</a>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
            </div>
            <!--      <div class="layui-trans layui-form-item layadmin-user-login-other">-->
            <!--        <label>社交账号登入</label>-->
            <!--        <a href="javascript:;"><i class="layui-icon layui-icon-login-qq"></i></a>-->
            <!--        <a href="javascript:;"><i class="layui-icon layui-icon-login-wechat"></i></a>-->
            <!--        <a href="javascript:;"><i class="layui-icon layui-icon-login-weibo"></i></a>-->
            <!--        -->
            <!--        <a lay-href="/user/reg" class="layadmin-user-jump-change layadmin-link">注册帐号</a>-->
            <!--      </div>-->
        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© All Rights Reserved</p>
    </div>

    <!--<div class="ladmin-user-login-theme">
      <script type="text/html" template>
        <ul>
          <li data-theme=""><img src="{{ layui.setter.paths.base }}style/imgs/bg-none.jpg"></li>
          <li data-theme="#03152A" style="background-color: #03152A;"></li>
          <li data-theme="#2E241B" style="background-color: #2E241B;"></li>
          <li data-theme="#50314F" style="background-color: #50314F;"></li>
          <li data-theme="#344058" style="background-color: #344058;"></li>
          <li data-theme="#20222A" style="background-color: #20222A;"></li>
        </ul>
      </script>
    </div>-->

</div>

<script>
    layui.use(['admin', 'form', 'user'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router()
            , search = router.search;

        form.render();

        let sendEmailVerify = function () {
            var isValid = form.validate('#LAY-user-login-username');  // 主动触发验证，v2.7.0 新增
            // 验证通过
            if (isValid) {
                // 此处可继续书写「发送验证码」等后续逻辑
                // …
                let v = "layui-disabled"
                    , elem = $('#LAY-user-login-getsmscode')
                    , seconds = 59 // 倒数秒数
                elem.addClass(v).off('click')

                let t = setInterval(() => {
                    if (seconds === 0) {
                        elem.text("获取验证码")
                        elem.removeClass(v).on('click', sendEmailVerify)
                        clearInterval(t)
                    } else {
                        elem.text(seconds + "秒后再次发送");
                        seconds--;
                    }
                }, 1000)

                var formData = form.val('LAY-user-login');
                // 发送邮箱验证码
                admin.req({
                    url: window.globalConfig.url_email_verify,
                    type: "post",
                    data: {
                        email: formData.username,
                        scene: 1
                    },
                    success: (res) => {
                        layer.msg(res.msg)
                    }
                })
            }
        }

        $('#LAY-user-login-getsmscode').on('click', sendEmailVerify);

        //提交
        form.on('submit(LAY-user-login-submit)', function (obj) {
            let field = obj.field
            field.scene = 1;
            //请求登入接口
            admin.req({
                //url: './res/json/user/login.js' //实际使用请改成服务端真实接口
                url: window.globalConfig.url_login //实际使用请改成服务端真实接口
                , data: field
                , method: 'POST'
                , done: function (res) {
                    if (res.code !== 0){
                        layer.msg(res.msg)
                    }

                    //请求成功后，写入 access_token
                    layui.data(setter.tableName,
                        {
                            key: setter.request.tokenName
                            , value: res.data.access_token
                        }
                    );
                    layui.data(setter.tableName,
                        {
                            key: 'user_info'
                            , value: JSON.stringify(res.data.user_info)
                        }
                    );


                    //登入成功的提示与跳转
                    layer.msg('登录成功', {
                        offset: '15px'
                        , icon: 1
                        , time: 1000
                    }, function () {
                        location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/';
                    });
                }
            });

        });

    });
</script>
